<template>
    <ZkModalVue :title="title" :size="'sm'" :visible="visible" @cancel="handleCancel" @ok="handleOk">
        <a-form-model ref="form" :model="info" v-bind="formLayout" :rules="rules">
            <a-form-model-item :label="info.type === AuditStatusEnum.RESOLVE ? '审批意见' : '驳回意见' " prop="comment">
                <a-textarea v-model="info.comment" :autoSize="{ minRows: 4 }" :maxLength="500" />
            </a-form-model-item>
        </a-form-model>
    </ZkModalVue>
</template>
<script>
    import ZkModalVue from '@/appBase/ZkModal.vue';
    import { financeOrderAgree, financeOrderReject } from '@/api/order';
    import { AuditStatusEnum } from '../enum/enum';
    import { notEmptyRule } from '@/appBase/formRules';
    export default {
        name: "OrderAudit",
        components: {
            ZkModalVue
        },
        data() {
            return {
                title: '申请通过',
                id: null,
                visible: false,
                /** 布局配置 */
                formLayout: {
                    layout: 'horizontal',
                    labelCol: {
                        xs: { span: 24 },
                        sm: { span: 5 }
                    },
                    wrapperCol: {
                        xs: { span: 24 },
                        sm: { span: 18 }
                    }
                },
                AuditStatusEnum,
                info: {
                    orderId: '',
                    type: null,
                    comment: '',
                },
                rules: {
                    comment: [notEmptyRule()],
                },
            }
        },
        methods: {
            /**
             * @param { Object } param
             * @param { String } param.id 订单id
             * @param { Number } param.type 订单审批状态枚举 AuditStatusEnum
             * @param { Number } param.authType 订单审批类型 1 || undefined=默认 2=开票退票换票
             * @param { Object } param.authParam 订单审批参数对象
             * @param { Function } param.onOk
             * @param { Function } param.onCancel
             */
            show(param = {}) {
                const { id, type, onOk, onCancel, authType, authParam } = param;
                this._onOk = onOk;
                this._onCancel = onCancel;
                this.title = type === AuditStatusEnum.RESOLVE ? '申请通过' : '申请驳回';
                this.visible = true;
                this.info.orderId = id;
                this.info.type = type;
                this.authType = authType;
                this.authParam = authParam;
                this.info.comment = type === AuditStatusEnum.RESOLVE ? '审批通过' : '';
            },
            handleOk() {
                this.$refs.form.validate( async(valid) => {
                    try {
                        if (!valid) return;
                        const { orderId, type, comment } = this.info;
                        const { authType, authParam } = this;
                        // 订单审批
                        const param = {
                            orderId,
                            comment,
                        }
                        if (type === AuditStatusEnum.RESOLVE) {
                            // 默认审批通过
                            await financeOrderAgree(param);
                        } else {
                            await financeOrderReject(param);
                        }
                        this.visible = false;
                        if (this._onOk) this._onOk();
                    } catch (error) {
                        console.error(error);
                    }
                });
            },
            handleCancel() {
                this.visible = false;
                if (this._onCancel) this._onCancel();
            }
        }
    }
</script>